package com.offcn.test;

import sun.misc.Unsafe;

import java.util.concurrent.TimeUnit;

/**
 * 能过唤醒线程
 */
public class UnsafeTest4 {
    public static void main(String[] args) throws InterruptedException {
        Unsafe unsafe = UnsafeUtil.getUnsafe();
        Thread t = new Thread(()->{
            for (int i=1;;i++){
                System.out.println("ThreadA:" + i);
                if (i%3==0){
                    System.out.println("park");
                    unsafe.park(true,6000000000000l);
                }
                try{
                    TimeUnit.SECONDS.sleep(1);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        t.start();
        while (true){
            //主线程等待6s,就唤醒线程
            TimeUnit.SECONDS.sleep(6);
            //唤醒子线程
            unsafe.unpark(t);
        }
    }
}
